<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="mode==='add'?'新增云音响':'编辑云音响'"
    :close-on-click-modal="false"
    width="750px"
    top="10vh"
    @close="close"
  >
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="120px" size="small">
      <el-form-item label="所属店铺：" prop="shopId">
        <el-select v-model="form.shopId" filterable clearable placeholder="可通过名称搜索">
          <el-option
            v-for="item in shopOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="云音响名称：" prop="name">
        <el-input v-model="form.name" maxlength="15" show-word-limit placeholder="请输入" />
      </el-form-item>

      <el-form-item label="品牌：" prop="brandType">
        <el-radio-group v-model="form.brandType">
          <el-radio
            v-for="(val,key,index) in brandOptions"
            :key="index"
            :label="Number(key)"
          >{{ val }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="form.brandType===1000" label="接口版本：" prop="model">
        <el-radio v-model="form.model" label="V1">V1版本</el-radio>
        <el-radio v-model="form.model" label="V2">V2版本</el-radio>
      </el-form-item>

      <el-form-item label="第三方SN号：" prop="supplierDeviceSn">
        <el-input v-model="form.supplierDeviceSn" maxlength="50" show-word-limit placeholder="请输入SN号" />
      </el-form-item>

      <el-form-item label="备注：" prop="tag">
        <el-input v-model="form.remark" maxlength="30" placeholder="说明备注" />
      </el-form-item>

      <el-form-item label="启用：" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="2"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSpeaker, editSpeaker } from '@/api/mch/equipment'
import { allShopList } from '@/api/mch/shop'

export default {
  data() {
    return {
      // 校验字段相关
      rules: {
        shopId: [
          {
            required: true,
            message: '请选择组织架构',
            trigger: ['change']
          }
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: ['blur', 'change']
          }
        ],
        brandType: [
          {
            required: true,
            message: '请选择品牌',
            trigger: ['change']
          }
        ],
        supplierDeviceSn: [
          {
            required: true,
            message: '请输入第三方SN号',
            trigger: ['blur', 'change']
          }
        ],
        status: [
          {
            required: true,
            message: '请选择',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 配置
      title: null,
      resolve: null,
      reject: null,
      brandOptions: {
        1000: '智联博众',
        2000: '博实结'
      },
      // 状态
      dialogVisible: false,
      loading: false,
      // 数据
      mode: 'add',
      shopOptions: [],
      form: {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1000,
        model: '',
        supplierDeviceSn: '',
        remark: ''
      }
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {

  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    // 弹框相关
    open({ mode = 'add', row = null }) {
      this.getAllShopOptions()
      this.mode = mode
      if (mode === 'edit') {
        this.form = Object.assign({}, row)
      }
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    close() {
      this.form = {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1000,
        model: '',
        supplierDeviceSn: '',
        remark: ''
      }
      this.$refs.form.resetFields()
      this.loading = false
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.mode === 'add' ? this.add() : this.edit()
        }
      })
    },

    /**
     * 数据组
     */
    // 获取所有门店
    getAllShopOptions() {
      const params = {
        businessId: this.$route.query.mchId
      }
      this.loading = true
      allShopList({ params }).then(res => {
        this.shopOptions = res.data || []
      }).finally(() => {
        this.loading = false
      })
    },

    /**
     * 按钮组
     */
    // 新增
    add() {
      this.loading = true
      const params = Object.assign({}, this.form)
      params.businessId = this.$route.query.mchId
      addSpeaker({ data: params }).then(() => {
        this.$root.$emit('refreshMchEquipmentSpeakerList')
        this.$notify({
          title: '成功',
          message: '新增成功',
          type: 'success',
          duration: 2000,
          offset: 40
        })
        this.hide()
      }).finally(() => {
        this.loading = false
      })
    },

    // 编辑
    edit() {
      this.loading = true
      const params = Object.assign({}, this.form)
      editSpeaker({ data: params }).then(() => {
        this.$root.$emit('refreshMchEquipmentSpeakerList')
        this.$notify({
          title: '成功',
          message: '更新成功',
          type: 'success',
          duration: 2000,
          offset: 40
        })
        this.hide()
      }).finally(() => {
        this.loading = false
      })
    }
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-select {
  width: 100%;
}

.el-checkbox {
  width: 120px;
}
</style>
